<?php
/**
 * Created by PhpStorm.
 * User: blue
 * Date: 2017/9/2
 * Time: 16:48
 */

namespace App\Contacts\Repository;


use Illuminate\Container\Container as App;


abstract class Eloquent implements Repository
{
    private $app;
    private $model;

    public function __construct(App $app)
    {
        $this->app = $app;
        $this->makeModel();
    }

    /**
     * 设置model
     * @return mixed
     */
    public abstract function model();

    /**
     * 返回model
     * @return mixed
     */
    public function makeModel(){

        $model = $this->app->make($this->model());

        /*if (!class_exists($model)){//检测绑定模型是否存在
            throw new \Exception("Model".$model." not exists");
        }*/

        return $this->model = $model;
    }

    /**
     * @param array $columns
     * @return mixed
     */
    public function all($columns = array('*'))
    {
        return $this->model->all($columns);
    }

    /**
     * @param null $limit
     * @param array $columns
     * @return mixed
     */
    public function paginate($limit = null, $columns = ['*'])
    {
        return $this->model->paginate($limit, $columns);
    }

    /**
     * @param $id
     * @param array $columns
     * @return mixed
     */
    public function find($id, $columns = ['*'])
    {
        return $this->model->find($id, $columns);
    }

    /**
     * @param array $attributes
     * @return mixed
     */
    public function create(array $attributes)
    {
        return $this->model->create($attributes);
    }

    /**
     * @param array $attributes
     * @param $id
     * @return mixed
     */
    public function update(array $attributes, $id)
    {
        return $this->model->find($id)->update($attributes);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function delete($id)
    {
        return $this->model->destory($id);
    }
}